package com.qimou.iems.interceptor;

import com.qimou.iems.entity.AdminUser;
import com.qimou.iems.util.LoginHelper;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String uri = httpServletRequest.getRequestURI();
        if(uri.contains("login")){
            return true;
        }
        Object attribute = httpServletRequest.getSession().getAttribute(LoginHelper.LOGIN_SESSION);
        if(null == attribute || !(attribute instanceof AdminUser)){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/page/login");
            return false;
        }

        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
